﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace JLPT.Models.DAL
{
    public class DBAccess
    {
        public static bool Update<T>(T item, 
            params string[] changedPropertyNames) where T  : class, new()
        {
            using(var db = new JLPTContext()){
                db.Set<T>().Attach(item);
                foreach (var propertyName in changedPropertyNames)
                {
                    // If we can't find the property, this line wil throw an exception, 
                    //which is good as we want to know about it
                    db.Entry(item).Property(propertyName).IsModified = true;
                }
                db.SaveChanges();
            }
            return true;
        }

        public static bool Delete<T>(T item) where T : class, new()
        {
            using (var db = new JLPTContext())
            {
                db.Set<T>().Attach(item);
                db.Set<T>().Remove(item);
                db.SaveChanges();
            }
            return true;
        }
    }
}